package com.zj.fx.controller.month;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zj.fx.common.base.BaseController;
import com.zj.fx.common.domain.AjaxResult;
import com.zj.fx.common.domain.ResultTable;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.model.month.MonthConstant;
import com.zj.fx.model.month.MonthResult;
import com.zj.fx.service.month.MonthResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 月评价结果Controller
 *
 * @author SQL
 * @ClassName: MonthResultController
 * @date 2023-06-20 17:32:45
 */
@Api(value = "月评价结果")
@Controller
@RequestMapping("/MonthResultController")
public class MonthResultController extends BaseController {

    private String prefix = "month/monthResult";

    @Autowired
    private MonthResultService monthResultService;


    /**
     * 月评价结果页面展示
     *
     * @param model
     * @return String
     * @author SQL
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/view")
    @RequiresPermissions("gen:monthResult:view")
    public String view(ModelMap model) {
        return prefix + "/list";
    }

    /**
     * list集合（分页）
     *
     * @param tablepar
     * @param
     * @return
     */
    //@Log(title = "月评价结果", action = "111")
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/list")
    @RequiresPermissions("gen:monthResult:list")
    @ResponseBody
    public ResultTable list(Tablepar tablepar, MonthResult monthResult) {
        //分页查询
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        monthResult.setIsIssue(MonthConstant.ISSUE_FINISH);
        List<MonthResult> monthResults = monthResultService.list4Score(tablepar, monthResult);
        monthResults = monthResults.stream().sorted(Comparator.comparingInt(x->Integer.valueOf(x.getFinalrank()))).collect(Collectors.toList());
//        for (int i = 1; i <= monthResults.size(); i++) {
//            monthResults.get(i-1).setRank(String.valueOf(i));
//        }
        PageInfo<MonthResult> pageInfo = new PageInfo<MonthResult>(monthResults);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 新增跳转
     */
    @ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        return prefix + "/add";
    }

    /**
     * 新增保存
     *
     * @param
     * @return
     */
    //@Log(title = "月评价结果新增", action = "111")
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    @RequiresPermissions("gen:monthResult:add")
    @ResponseBody
    public AjaxResult add(MonthResult monthResult) {
        int b = monthResultService.insertSelective(monthResult);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 月评价结果删除
     *
     * @param ids
     * @return
     */
    //@Log(title = "月评价结果删除", action = "111")
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/remove")
    @RequiresPermissions("gen:monthResult:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = monthResultService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }


    /**
     * 修改跳转
     *
     * @param id
     * @param
     * @return
     */
    @ApiOperation(value = "修改跳转", notes = "修改跳转")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map) {
        map.put("MonthResult", monthResultService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    //@Log(title = "月评价结果修改", action = "111")
    @ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:monthResult:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MonthResult monthResult) {
        return toAjax(monthResultService.updateByPrimaryKeySelective(monthResult));
    }

    /**
     * 营销部月评价管理页面查看实时结果
     *
     * @param tablepar
     * @param
     * @return
     */
    //@Log(title = "月评价结果", action = "111")
    @ApiOperation(value = "营销部月评价管理页面查看实时结果", notes = "营销部月评价管理页面查看实时结果")
    @GetMapping("/list4currentResult/{taskMonth}")
    @ResponseBody
    public ResultTable list4currentResult(@PathVariable("taskMonth") String taskMonth,Tablepar tablepar, MonthResult monthResult) {
        monthResult.setTaskMonth(taskMonth);
        //分页查询
//        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<MonthResult> monthResults = monthResultService.list4Score(tablepar, monthResult);
        monthResults = monthResults.stream().sorted(Comparator.comparing(MonthResult::getTotalScore).reversed()).collect(Collectors.toList());
        for (int i = 1; i <= monthResults.size(); i++) {
            monthResults.get(i-1).setFinalrank(String.valueOf(i));
        }
        PageInfo<MonthResult> pageInfo = new PageInfo<MonthResult>(monthResults);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * list集合-首页展示
     *
     * @param monthResult
     * @param
     * @return
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/list4index")
    @ResponseBody
    public Map<String, Object> list(MonthResult monthResult) {
        Map<String, Object> map = new HashMap<>();
        monthResult.setIsIssue(MonthConstant.ISSUE_FINISH);
        List<MonthResult> monthResults = monthResultService.list4Score(monthResult);
        monthResults = monthResults.stream().sorted(Comparator.comparingInt(x->Integer.valueOf(x.getFinalrank()))).collect(Collectors.toList());
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", monthResults.size());
        map.put("data", monthResults);
        return map;
    }


}
